package com.zerui.manager.icc.service;

import com.alibaba.fastjson.JSONObject;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.zerui.manager.icc.domain.CardInfo;
import com.zerui.manager.icc.properties.ICCProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("iccCardInfoService")
public class CardInfoService extends AbstractBase {

    @Resource
    private ICCProperties iccProperties;

    /**
     * 新增卡号
     *
     * @param cardInfo ICC卡信息
     * @return
     * @throws ClientException
     */
    public CardInfo add(CardInfo cardInfo) throws ClientException {
        Assert.notNull(cardInfo.getCardNumber(), "卡号不能为空");
        Assert.notNull(cardInfo.getDepartmentId(), "所属部门不能为空");
        Assert.notNull(cardInfo.getCardType(), "卡类型不能为空");
        Assert.notNull(cardInfo.getCategory(), "卡介质不能为空");
        Assert.notNull(cardInfo.getStartDate(), "开始日期不能为空");
        Assert.notNull(cardInfo.getEndDate(), "截止日期不能为空");
        this.generalRequest(this.iccProperties.getCard().getAddUrl(), Method.POST, cardInfo, "data");
        return cardInfo;
    }

    /**
     * 更新卡片信息
     *
     * @param cardInfo 卡片信息
     * @return
     * @throws ClientException
     */
    public CardInfo update(CardInfo cardInfo) throws ClientException {
        Assert.notNull(cardInfo.getCardNumber(), "卡号不能为空");
        Assert.notNull(cardInfo.getDepartmentId(), "所属部门不能为空");
        Assert.notNull(cardInfo.getIsCoercion(), "是否为胁迫卡不能为空");
        Assert.notNull(cardInfo.getStartDate(), "开始日期不能为空");
        Assert.notNull(cardInfo.getEndDate(), "截止日期不能为空");
        this.generalRequest(this.iccProperties.getCard().getUpdateUrl(), Method.PUT, cardInfo, "data");
        return cardInfo;
    }

    /**
     * 删除指定卡号的信息
     *
     * @param cardNumbers 卡号集合
     * @return
     * @throws ClientException
     */
    public Boolean delete(String... cardNumbers) throws ClientException {
        Assert.notNull(cardNumbers, "请指定卡片号码");
        Map<String, Object> joParam = new HashMap<>();
        joParam.put("cardNumbers", Arrays.asList(cardNumbers));
        this.generalRequest(this.iccProperties.getCard().getDeleteUrl(), Method.DELETE, joParam, "data");
        return true;
    }

    /**
     * 检查是否存在相同的卡号
     *
     * @param cardNum 卡号
     * @return
     * @throws ClientException
     */
    public Boolean checkCardNum(final String cardNum) throws ClientException {
        Map<String, String> joParam = new HashMap<>();
        joParam.put("cardNumber", cardNum);
        JSONObject joData = this.generalRequest(this.renderUrl(this.iccProperties.getCard().getDetailUrl(), joParam), Method.GET, null, "data");
        List<CardInfo> cardInfos = this.convert2TypeData(joData, CardInfo.class);
        return !cardInfos.isEmpty();
    }

    /**
     * 查询指定卡号的卡详情
     *
     * @param cardNum 卡号
     * @return
     * @throws ClientException
     */
    public CardInfo selectByCardNum(String cardNum) throws ClientException {
        Assert.notNull(cardNum, "请指定卡片号码");
        Map<String, String> joParam = new HashMap<>();
        joParam.put("cardNumber", cardNum);
        JSONObject joData = this.generalRequest(this.renderUrl(this.iccProperties.getCard().getDetailUrl(), joParam), Method.GET, null, "data");
        List<CardInfo> cardInfos = this.convert2TypeData(joData, CardInfo.class);
        return cardInfos.size() > 0 ? cardInfos.get(0) : null;
    }
}
